﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using CLEX.Service.DataContracts;

namespace CLEX.Client.Metro.ViewModel.Lattice
{
    public class HistogramNewVM : VMBase
    {
        private readonly IPopupService popup;
        private readonly LatticeState state;
        private readonly List<AggregateTypeData> aggregateTypes;
        private readonly List<string> columns;
        private AggregateTypeData aggregateType;
        private string column;
        private readonly RelayCommand create;
        private readonly RelayCommand cancel;

        public IEnumerable<AggregateTypeData> AggregateTypes
        {
            get { return aggregateTypes; }
        }

        public IEnumerable<string> Columns
        {
            get { return columns; }
        }

        public AggregateTypeData AggregateType
        {
            get { return aggregateType; }
            set
            {
                if (aggregateType != value)
                {
                    aggregateType = value;
                    RaisePropertyChanged("AggregateType");
                    create.RaiseCanExecuteChanged();
                }
            }
        }

        public string Column
        {
            get { return column; }
            set
            {
                if (column != value)
                {
                    column = value;
                    RaisePropertyChanged("Column");
                    create.RaiseCanExecuteChanged();
                }
            }
        }

        public ICommand Create
        {
            get { return create; }
        }

        public ICommand Cancel
        {
            get { return cancel; }
        }

        public HistogramNewVM(IPopupService popup, TableData table, LatticeState state)
        {
            this.popup = popup;
            this.state = state;
            this.aggregateTypes = Enum.GetValues(typeof(AggregateTypeData)).Cast<AggregateTypeData>().ToList();
            this.columns = table.Columns.Select(p => p.Name).ToList();
            this.create = new RelayCommand(DoCreate);
            this.cancel = new RelayCommand(DoCancel);
            this.column = this.columns.First();
            this.aggregateType = this.aggregateTypes.First();
        }

        private void DoCreate()
        {
            state.AddAggregate(new AggregateData(aggregateType, column));
            popup.ClosePopup();
        }

        private void DoCancel()
        {
            popup.ClosePopup();
        }
    }
}
